RandomPassword

Random Password Generator

Another code snippet where we are going to generate random password which accepts user input by use two libraries: * random * string

For instance, when the program runs, it will ask the user to input how many number of password and the length of password to be generated. to generate a combiantion of letters, numbers and special keys to generate random passwords for us to use

Sample Output:

Source Code: password.py

import string
import secrets

# Define the set of characters to be used in the password
CHARACTER_SET = string.ascii_letters + string.digits + string.punctuation

def generate_password(length):
    """Generate a random password of the specified length."""
    password = ''.join(secrets.choice(CHARACTER_SET) for i in range(length))
    return password

def main():
    # Prompt the user for the number of passwords to generate and their length
    while True:
        try:
            num_pass = int(input("How many passwords do you want to generate? "))
            password_length = int(input("Enter the length of the password(s): "))
            break
        except ValueError:
            print("Please enter a valid integer.")
            continue 
    # Generate the specified number of passwords and print them to the console
    print("Generated passwords:")
    for i in range(num_pass):
        password = generate_password(password_length)
        print(f"{i+1}. {password}")

if __name__ == "__main__":
    main()